/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Connection;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author My PC
 */
public class DataAccess {
    Connection connection;
      Connect con;
       public DataAccess()
    {
          Connect a = new Connect ();
           connection = a.getConnectToSQL();
    }
    public int executeUpdate(String sql) throws SQLException
    {
        Statement st=connection.createStatement();
        int kq=st.executeUpdate(sql);
        return kq;
    }
    public ResultSet executeQuery(String sql) throws SQLException
    {
        Statement st=connection.createStatement();
        ResultSet rs=st.executeQuery(sql);
        return rs;
    }
    public void ShowInfor(String sql,JTable jTable){
        try{
        //String sql="Select * from  "+table_;
        DefaultTableModel table = new DefaultTableModel();
        ResultSet rs=this.executeQuery(sql);
        ResultSetMetaData rsmd=rs.getMetaData();
        int colums=rsmd.getColumnCount();
        Vector colum_name=new Vector();
        Vector data_rows=new Vector();
        for(int i=1;i<=colums;i++){
            colum_name.addElement(rsmd.getColumnName(i));
        }
        table.setColumnIdentifiers(colum_name);
        while(rs.next()){
            data_rows=new Vector();
            for (int j = 1; j <= colums; j++) {
                data_rows.addElement(rs.getString(j));
            }
            table.addRow(data_rows);
        }
        jTable.setModel(table);
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
}
